'use strict';

module.exports = app => {
  class Weapp extends app.Service  {
    async getopenid(code){
        let https = require('https');
        let iconv = require("iconv-lite");
        let url = "https://api.weixin.qq.com/sns/jscode2session?appid="+this.app.config.weappSDK.appId+"&secret="+this.app.config.weappSDK.appSecret+"&js_code="+code+"&grant_type=authorization_code";
        return new Promise(function(resolve, reject) {
            https.get(url,(res)=>{
                res.setEncoding('utf8');
                let rawData = '';
                res.on('data', (chunk) => rawData += chunk);

                res.on('end', () => {
                    resolve(rawData);
                });
            });
        });
    }
  }
  return Weapp;
};